<?php

namespace App\Services;

use App\AppChannel;

/**
 * Class BaseService
 *
 * @package \App\Services
 */
class BaseService
{
    /**
     * 生成唯一code
     * @return string
     */
    public function shortUrl()
    {
        $code = [
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        ];
//        $uniqueCode = $code[intval(date('Y')) - 2019] . strtoupper(dechex(date('m'))) . date(
//                'd') . substr(time(), -5) . sprintf(
//                '%s', $code[rand(0, 52)]);

        // 修改下生成逻辑
        //$uniqueCode = $code[rand(0, 51)] . rand(10, 99) . $code[rand(0, 51)] . rand(0, 9) . $code[rand(0, 51)];

        $uniqueCode = $code[rand(0, 51)] . $code[rand(0, 51)] . rand(10, 99) . $code[rand(0, 51)];

        // 验证表里面存不存在
        $flag = true;
        do {
            $res = AppChannel::where("short_url_word", $uniqueCode)->first(["id"]);
            if (empty($res)) {
                $flag = false;
            } else {
                //$uniqueCode = $code[rand(0, 51)] . rand(10, 99) . $code[rand(0, 51)] . rand(0, 9) . $code[rand(0, 51)];

                $uniqueCode = $code[rand(0, 51)]  . $code[rand(0, 51)] . rand(10, 99) . $code[rand(0, 51)];
            }
        } while($flag);

        // 随机取一个
        return $uniqueCode;
    }


    public function getHost(string $url)
    {
        $parts = parse_url($url);
        if ($parts === false || ! isset($parts['host'])) {
            return '';
        }
        return $parts['host'];
    }

    /**
     * POST 请求
     *
     * @param $url
     * @param $params
     *
     * @return mixed
     */
    public function curlPost($url, $params)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); //  如果不是https的就注释 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $res = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        $result = json_decode($res,true);

        return $result;
    }


    /**
     * GET 请求
     *
     * @param $url
     *
     * @return bool|string
     */
    public function curlGet($url)
    {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}
